package com.changgou.backend.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.feign.SpecFeign;
import com.changgou.goods.pojo.Spec;
import com.changgou.goods.pojo.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/wspec")
public class SpecController {

    @Autowired
    private SpecFeign specFeign;

    //查询规格列表
    @PostMapping("/search")
    @ResponseBody
    public Result findPage(@RequestBody Map<String,String> map){
        String page = map.get("page");
        String size = map.get("size");
        String templateName = map.get("templateName");
        System.out.println(map);
        PageResult pageResult = specFeign.findPage(map, Integer.parseInt(page), Integer.parseInt(size)).getData();
        List<Map> rows = pageResult.getRows();
        for (Map row : rows) {
            row.put("templateName",templateName);
        }
        return new Result(true, StatusCode.OK,"查询规格列表成功",pageResult);
    }

    //添加规格列表
    @PostMapping
    @ResponseBody
    public Result add(@RequestBody Spec spec,Integer id){
        spec.setTemplateId(id);
        specFeign.add(spec);
        return new Result(true,StatusCode.OK,"添加规格列表成功");
    }

    //根据id查询规格列表 回显数据
    @GetMapping("/{id}")
    @ResponseBody
    public Result<Spec> findById(@PathVariable("id") Integer id){
        Spec spec = specFeign.findById(id).getData();
        return new Result(true,StatusCode.OK,"根据id查询规格列表成功",spec);
    }

    //修改规格列表
    @PutMapping
    @ResponseBody
    public Result update(@RequestBody Spec spec){
        specFeign.update(spec,spec.getId());
        return new Result(true,StatusCode.OK,"修改规格列表成功");
    }

    //删除规格列表
    @DeleteMapping("/{id}")
    @ResponseBody
    public Result delete(@PathVariable("id") Integer id){
        specFeign.delete(id);
        return new Result(true,StatusCode.OK,"删除规格列表成功");
    }

}
